package userlimitController

import (
	"arabApi/app/api/controllers"
	"arabApi/app/api/controllers/CFun"
	"arabApi/app/api/requests"
	"arabApi/app/api/requests/CRequest"
	"arabApi/app/api/requests/userlimitRequest"
	"arabApi/app/api/services/userLimitService"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Menu struct {
	controllers.BaseAPIController
}

func (Menu) List(c *gin.Context) {

	r := requests.PageStruct{}
	if err := c.ShouldBindQuery(&r); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("解析错误"+err.Error()))
		return
	}

	list, count := userLimitService.MenuList(r)
	data := gin.H{}
	data["list"] = list
	data["count"] = count
	CFun.Ajax_return_success(c, CFun.WithData(data))
}

func (Menu) Add(c *gin.Context) {

	r := userlimitRequest.MenuAdd{}
	if err := c.ShouldBindBodyWith(&r, binding.JSON); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("解析错误"+err.Error()))
		return
	}

	if ok := requests.Validate(c, &r, userlimitRequest.CheckMenu); !ok {
		return
	}

	if ok := userLimitService.MenuAdd(r); ok != 0 {
		CFun.Ajax_return_fail(c, CFun.WithMsg("添加失败"))
	} else {
		CFun.Ajax_return_success(c, CFun.WithMsg("添加成功"))
	}

}

func (Menu) Edit(c *gin.Context) {
	r := userlimitRequest.MenuAdd{}
	if err := c.ShouldBindBodyWith(&r, binding.JSON); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("解析错误"+err.Error()))
		return
	}

	if ok := requests.Validate(c, &r, userlimitRequest.CheckMenu); !ok {
		return
	}

	if ok := userLimitService.MenuEdit(r); ok != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("编辑失败"+ok.Error()))
	} else {
		CFun.Ajax_return_success(c, CFun.WithMsg("编辑成功"))
	}
}

func (Menu) Msg(c *gin.Context) {

	request := CRequest.Oid{}

	// 解析 JSON 请求
	if err := c.ShouldBindQuery(&request); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg(err.Error()))
		return
	}

	if ok := requests.Validate(c, &request, CRequest.CheckID); !ok {
		return
	}

	system := userLimitService.MenurMsg(request.Id)
	CFun.Ajax_return_success(c, CFun.WithData(system))

}
